package com.hedl.classroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hedl.classroom.base.exception.OnlinecClassroomException;
import com.hedl.classroom.mapper.MediaFilesMapper;
import com.hedl.classroom.mapper.MediaProcessHistoryMapper;
import com.hedl.classroom.mapper.MediaProcessMapper;
import com.hedl.classroom.model.po.MediaFiles;
import com.hedl.classroom.model.po.MediaProcess;
import com.hedl.classroom.model.po.MediaProcessHistory;
import com.hedl.classroom.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * ProjectName: onlinec-classroom
 * DateTime: 2023/7/18 00:06
 * Author: Big Watermelon
 * Description:
 */
@Slf4j
@Service
public class MediaFileProcessServiceImpl implements MediaFileProcessService {

    @Resource
    private MediaProcessMapper mediaProcessMapper;

    @Resource
    private MediaFilesMapper mediaFilesMapper;

    @Resource
    private MediaProcessHistoryMapper mediaProcessHistoryMapper;

    @Override
    public List<MediaProcess> getMediaProcessList(int shardTotal, int shardIndex, int count) {

        return mediaProcessMapper.selectListByShardIndex(shardTotal,shardIndex,count);
    }

    @Override
    public boolean startTask(long id) {
        int result = mediaProcessMapper.startTask(id);
        return result <= 0 ? false : true;
    }

    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        //更新任务
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if (mediaProcess == null){
            return;
        }
//        //如果任务执行失败
//        if(status.equals("3")){
//            //更新MediaProcess表的状态
//            mediaProcess.setStatus("3");
//            mediaProcess.setFailCount(mediaProcess.getFailCount()+1);
//            mediaProcess.setErrormsg(errorMsg);
//            mediaProcessMapper.updateById(mediaProcess);
//        }
//
//        //如果任务执行成功
//        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
//        //更新media_file中的url
//        mediaFiles.setUrl(url);
//        mediaFilesMapper.updateById(mediaFiles);
//
//        //更新MediaProcess表的状态
//        mediaProcess.setStatus("2");
//        mediaProcess.setFinishDate(LocalDateTime.now());
//        mediaProcess.setUrl(url);
//        mediaProcessMapper.updateById(mediaProcess);
//        //将MediaProcess表记录插入到MediaProcessHistory表
//        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
//        BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
//        int insert = mediaProcessHistoryMapper.insert(mediaProcessHistory);
//        if(insert <= 0){
//            OnlinecClassroomException.cast("插入表记录失败！");
//        }
//        //从MediaProcess删除当前任务
//        mediaProcessMapper.deleteById(taskId);

        LambdaQueryWrapper<MediaProcess> queryWrapper = new LambdaQueryWrapper<MediaProcess>().eq(MediaProcess::getId, taskId);
        // 如果任务失败
        if ("3".equals(status)) {
            log.debug("任务失败：{}", taskId);
            MediaProcess mediaProcess_u = new MediaProcess();
            mediaProcess_u.setStatus("3");
            mediaProcess_u.setErrormsg(errorMsg);
            mediaProcess_u.setFinishDate(LocalDateTime.now());
            mediaProcess_u.setFailCount(mediaProcess.getFailCount()+1);
            mediaProcessMapper.update(mediaProcess_u, queryWrapper);
            return;
        }
        // 任务成功，将其从待处理任务表中删除，同时新增历史处理表记录
        if ("2".equals(status)) {
            mediaProcess.setStatus("2");
            mediaProcess.setUrl(url);
            mediaProcess.setFinishDate(LocalDateTime.now());
            mediaProcessMapper.update(mediaProcess, queryWrapper);
            MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
            // 两张表的属性完全一致，直接拷贝
            BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
            // 向历史处理表新增数据
            mediaProcessHistoryMapper.insert(mediaProcessHistory);
            // 同时删除待处理任务表中的数据
            mediaProcessMapper.deleteById(taskId);
        }
    }
}
